package com.zlx.xbhy.controller;

import com.alibaba.fastjson.JSONObject;
import com.sun.deploy.net.HttpRequest;
import com.sun.deploy.net.HttpResponse;
import com.zlx.xbhy.entity.User;
import com.zlx.xbhy.service.UserService;
import com.zlx.xbhy.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * @ClassName WeChatController
 * @Description 微信登录
 * @Author Edward
 * @Date 2020/11/24 23:54
 */
@RestController
@RequestMapping("/WeChat/")
public class WeChatController {
    @Autowired
    private UserService userService;

    @RequestMapping("towx_login")
    public void towx_login(HttpServletResponse response){
        String appid = "wx3500b25b23938d73";
        String redirectUri = "http://ktc.dfbz.com/WeChat/wx_login";
        String appSecret = "56ee69a0faf098b3ce3c808cc6dac1ff";
        String url = "https://open.weixin.qq.com/connect/qrconnect" +
                "?appid=" + appid +
                "&redirect_uri=" + redirectUri +
                "&response_type=code" +
                "&scope=snsapi_login";
        try {
            response.sendRedirect(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("wx_login")
    public void wx_login(HttpServletResponse response, HttpServletRequest request) {
        HttpSession session = request.getSession();


        String code = request.getParameter("code");


        // https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

        String appid = "wx3500b25b23938d73";
        String AppSecret = "56ee69a0faf098b3ce3c808cc6dac1ff";

        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid +
                "&secret=" + AppSecret +
                "&code=" + code +
                "&grant_type=authorization_code";

        // 获取AccessToken、openid等数据
        JSONObject authInfo = HttpUtils.auth(url);
//        System.out.println("authInfo: " + authInfo);
        url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + authInfo.getString("access_token") +
                "&openid=" + authInfo.getString("openid");
        //转换微信登陆的信息为Json字符串
        JSONObject userInfo = HttpUtils.getUserInfo(url);
//        System.out.println("userInfo: " + userInfo);

        String openid = userInfo.getString("openid");
        User user = new User();
        user.setWxOpenid(openid);
        user = userService.selectOne(user);
        if (user == null) {//添加用户
            user = new User();
            String sex = userInfo.getString("sex");
            String nickname = userInfo.getString("nickname");
            String headimgurl = userInfo.getString("headimgurl");
            user.setWxOpenid(openid);
            user.setGender(sex);
            user.setUsername(nickname);
            user.setIsSecret("0");
            user.setRegisterTime(new Date());
            user.setLoginTime(new Date());
            user.setRealName(nickname);
            user.setPic(headimgurl);
            userService.insertSelective(user);
            user = userService.selectOne(user);
        }
        session.setAttribute("loginUser", user);
        try {
            response.sendRedirect("/html/home.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
